import request from "./useRequests";
import { userinfo } from '@/stores/counter'
import { ElMessage } from 'element-plus'

export default function getSecondaryTitle() {
    const user = userinfo()
    async function getSearchCate() {
        try {
            const res = await request({
                method: "get",
                url: `/news/getSecondaryTitle`
            })
            type resultData = {
                belongTo: string,
                id: number,
                name: string
                createAt: string
            }
            const datas = res.data.data as resultData[]
            let cates: { [key: string]: any } = {};
            for (let i = 0; i < datas.length; i++) {
                // 检查cates是否有 res.data.data[i].belongTo 这个属性
                if (datas[i].belongTo == datas[i].name) {
                    cates[datas[i].belongTo] = {
                        id: datas[i].id,
                        name: datas[i].name
                    }
                }
                else {
                    if (!cates[datas[i].belongTo]) {
                        cates[datas[i].belongTo] = []
                    }
                    cates[datas[i].belongTo].push({
                        id: datas[i].id,
                        name: datas[i].name
                    })
                }
            }
            return cates

        } catch (err: any) {
            //alert(err)
        }
    }

    return {
        getSearchCate
    }
}